package com.sp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping
@RestController
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("/selectList")
    public List selectList() {
        //查数据库，数据，返回List
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select id,a_name,d_ct from a_role");
        return list;
    }

    @RequestMapping("/selectMap")
    public Map selectMap() {
        //查，返回一条数据，转成Map
        Map<String, Object> map = jdbcTemplate.queryForMap("select id,a_name,d_ct from a_role limit 1");
        return map;
    }

    //前端调用：    http://127.0.0.1:8099/selectListAndMap?school=吉林电子


    @RequestMapping("/selectListAndMap")
    public Map selectListAndMap(@RequestParam Map param) {
        Object a = "王";


        System.out.println(
                "select id,a_name,d_ct " +
                        "from a_role where " +
                        "a_name=" + a
        );

        System.out.println(
                "select id,a_name,d_ct " +
                        "from a_role where " +
                        "a_name='" + a + "'"
        );


//        //查数据库，数据，返回List
//        List<Map<String, Object>> list = jdbcTemplate.queryForList(
//                "select id,a_name,d_ct " +
//                        "from a_role where " +
//                        "a_name='" +  + "'"
//        );
        //        List<Map<String, Object>> list = jdbcTemplate.qu("select id,a_name,d_ct from a_role");

        //查，返回一条数据，转成Map
        //        Map<String, Object> map = jdbcTemplate.queryForMap("select id,a_name,d_ct from a_role limit 1");

        //        int update = jdbcTemplate.update("update a_role set a_name='1',d_ct='2' where id>10  ");
        //        int update = jdbcTemplate.update("insert into a_role set a_name='1',d_ct='2' where id>10  ");

        //        Map<String, Object> map = jdbcTemplate.u("select id,a_name,d_ct from a_role limit 1");
//        Map<String, Object> map = jdbcTemplate.queryForMap("select id,a_name,d_ct from a_role limit 1");

        //
        HashMap<Object, Object> data = new HashMap<>();
//        data.put("dataList", list);
//        data.put("dataMap", map);
        return data;
    }


//    @RequestMapping("/selectListAndMap")
//    public Map selectListAndMap(@RequestParam("school") String school) {
//        System.out.println(school);
////        //查数据库，数据，返回List
////        List<Map<String, Object>> list = jdbcTemplate.queryForList(
////                "select id,a_name,d_ct " +
////                        "from a_role where " +
////                        "a_name='" + school + "'"
////        );
//        //        List<Map<String, Object>> list = jdbcTemplate.qu("select id,a_name,d_ct from a_role");
//
//        //查，返回一条数据，转成Map
//        //        Map<String, Object> map = jdbcTemplate.queryForMap("select id,a_name,d_ct from a_role limit 1");
//
//        //        int update = jdbcTemplate.update("update a_role set a_name='1',d_ct='2' where id>10  ");
//        //        int update = jdbcTemplate.update("insert into a_role set a_name='1',d_ct='2' where id>10  ");
//
//        //        Map<String, Object> map = jdbcTemplate.u("select id,a_name,d_ct from a_role limit 1");
////        Map<String, Object> map = jdbcTemplate.queryForMap("select id,a_name,d_ct from a_role limit 1");
//
//        //
//        HashMap<Object, Object> data = new HashMap<>();
////        data.put("dataList", list);
////        data.put("dataMap", map);
//        return data;
//    }
}
